/***************************************************************************
Name: JSView
Description: Helps you to view the source code of external js and css files
Author: Ron Beckman
Homepage: http://forum.softwareblaze.com

Copyright (C) 2008  Ron Beckman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to:

Free Software Foundation, Inc.
51 Franklin Street
Fifth Floor
Boston, MA  02110-1301
USA
***************************************************************************/


var jsviewOptions = {

	getPrefs: function() {
		return Components.classes["@mozilla.org/preferences-service;1"].
		getService(Components.interfaces.nsIPrefService).
		getBranch('');
	},

	getStr: function(name) {
		return document.getElementById("jsview-bundle").getString(name);
	},

	init: function() {
		var prefs = this.getPrefs();
		var chkbxs = document.getElementsByTagName('checkbox');
		for (var i=0; i<chkbxs.length; ++i) {
			chkbxs[i].checked = prefs.getBoolPref(chkbxs[i].id);
		}

		var txtbxs = document.getElementsByTagName('textbox');
		for (var i=0; i<txtbxs.length; ++i) {
			txtbxs[i].value = prefs.getCharPref(txtbxs[i].id);
		}

		var mnulst = document.getElementsByTagName('menulist');
		for (var i=0; i<mnulst.length; ++i) {
			mnulst[i].selectedIndex = prefs.getIntPref(mnulst[i].id);
		}
	},

	exit: function() {
		var prefs = this.getPrefs();
		var chkbxs = document.getElementsByTagName('checkbox');
		for (var i=0; i<chkbxs.length; ++i) {
			prefs.setBoolPref(chkbxs[i].id,chkbxs[i].checked);
		}

		var txtbxs = document.getElementsByTagName('textbox');
		for (var i=0; i<txtbxs.length; ++i) {
			prefs.setCharPref(txtbxs[i].id,txtbxs[i].value);
		}

		var mnulst = document.getElementsByTagName('menulist');
		for (var i=0; i<mnulst.length; ++i) {
			prefs.setIntPref(mnulst[i].id,parseInt(mnulst[i].selectedIndex));
		}
	},

	resetPrefs: function() {
		var prefs = this.getPrefs();
		var chkbxs = document.getElementsByTagName('checkbox');
		for (var i=0; i<chkbxs.length; ++i) {
			if (prefs.prefHasUserValue(chkbxs[i].id)) {
				prefs.clearUserPref(chkbxs[i].id);
			}
		}

		var txtbxs = document.getElementsByTagName('textbox');
		for (var i=0; i<txtbxs.length; ++i) {
			if (prefs.prefHasUserValue(txtbxs[i].id)) {
				prefs.clearUserPref(txtbxs[i].id);
			}
		}

		var mnulst = document.getElementsByTagName('menulist');
		for (var i=0; i<mnulst.length; ++i) {
			if (prefs.prefHasUserValue(mnulst[i].id)) {
				prefs.clearUserPref(mnulst[i].id);
			}
		}
		this.init();
	},

	showHelp: function() {
		alert(this.getStr('jsview.FrPrfHlp'));
	},

	selectEditor: function() {
		var nsIFilePicker = Components.interfaces.nsIFilePicker;
		var fp = Components.classes["@mozilla.org/filepicker;1"].
        		   createInstance(nsIFilePicker);
		fp.init(window, this.getStr('jsview.SelectExt'), nsIFilePicker.modeOpen);
		fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterApps);

		//var os = navigator.platform;
		//if (os.indexOf('Mac') != -1) fp.appendFilter('app','*.app');
		var res = fp.show();
		if (res == nsIFilePicker.returnOK) {
			document.getElementById('view_source.editor.path').value = fp.file.path;
		}
	}
};